package com.test.jpa.dao;

import com.test.jpa.bean.Person;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;


public interface PersonDao extends JpaRepository<Person, Integer> {


    /**
     * 1.查询
     */
    //更多写法参考官方文档
    Person findByName(String name);

    //Using @Query
    @Query("select p from Person p where p.name = ?1")
    Person findPersonByName(String name);

    //Using Native Query
    @Query(value = "select * from person where name = ?1", nativeQuery = true)
    Person findPersonByName1(String name);

    //Using Named Parameters
    @Query("select p from Person p where p.name = :name")
    Person findPersonByName2(@Param("name") String name);

    //排序
    @Query("select p from Person p where p.name like ?1% order by ?2 asc")
    List<Person> findByAndSort(String name, String sort);

    //分页
    @Query("select p from Person p where p.name like ?1%")
    List<Person> findByAndPage(String name, Pageable pageable);

    //更新
    @Transactional
    @Modifying
    @Query("update Person p set p.name = ?1 where p.id = ?2")
    int updatePerson1(String name, Integer id);

    //删除
    @Transactional
    @Modifying
    @Query("delete from Person p where p.id = ?1")
    int delPerson(Integer id);









}
